/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ga_checkpoint_1;

import java.util.Arrays;

/**
 *  This class defines the entities that will be composing the population upon
 * which our genetic algorithm will be running on.
 * @author Asif Dipon, Harshal Joshi
 */
public class Entity implements Comparable{

    //the unique id of the Entity
    protected int id;
    //The entity's fitness, a calculated value
    protected double fitness;
    //The two element array containing the entity's genes.
    protected double[] genes;

    
    /**
     * Basic Constructor
     * @param id    The unique id
     * @param x1    The first gene
     * @param x2    The second gene
     */
    public Entity(int id, double x1, double x2){
        this.id = id;
        this.genes = new double[2];
        this.genes[0] = x1;
        this.genes[1] = x2;
        this.fitness = Math.pow(x1, 2) + Math.pow(x2, 2);
    }

    /**
     * Prompts the Entity to update its fitness.
     */
    public void updateFitness(){
        double x1 = this.genes[0];
        double x2 = this.genes[1];
        this.fitness = Math.pow(x1, 2) + Math.pow(x2, 2);
    }

    /**
     * Implementation of the compareTo function.
     * @param o
     * @return
     */
    public int compareTo(Object o) {
        Entity other = (Entity)o;
        double diff = this.fitness - other.fitness;
        if (diff >0) return 1;
        else if (diff < 0) return -1;
        else return 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Entity other = (Entity) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + this.id;
        hash = 97 * hash + (int) (Double.doubleToLongBits(this.fitness) ^ (Double.doubleToLongBits(this.fitness) >>> 32));
        hash = 97 * hash + Arrays.hashCode(this.genes);
        return hash;
    }


}
